/**
 *  草稿箱具体公文
 * */
var attach_document = false;
//调用方法获得参数

//获得参数的方法
var request =
    {
        QueryString : function(val)
        {
            var uri = window.location.search;
            var re = new RegExp("" +val+ "=([^&?]*)", "ig");
            return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
        }
    };

var doc_id = request.QueryString("doc_id");

$.ajax({
    type: 'GET',//请求类型
    url: '../document/draft/detailed/' + doc_id,//请求地址
    dataType: 'JSON',
    success: function (json,status){
        if(json.err_msg == null){
            // console.log(json);
            if(json.data!=null){
                $("#documentIframe").css("display","block");
                iFrameHeight();
                $("#documentIframe").attr("src","../assets/js/document/pdfjs-dist/web/viewer.html?file=/document/query/read/" + doc_id);
            }else{
                $("#draft_pdf").css("display","none");
                attach_document = true;
            }
        }else {
            alert(json.err_msg);
        }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
    },
    fail: function () { //如果在传输过程失败了：调用fail中对应的匿名函数(可选)
    }
});

$(document).ready(function () {

    //公文下载
    $("#documentDown").click(function () {
        //console.log
        window.location.href='../document/draft/down/' + doc_id;
    });

    //公文发送
    $("#sendDocument").click(function () {
        console.log("attach_document:" + attach_document);
        if(attach_document){
            window.location.href = '../document/my_doc_attach_new.html?doc_id='+ doc_id +'#step-3';
        }else {
            window.location.href = '../document/my_doc_new.html?doc_id='+ doc_id +'#step-3';
        }
    });


    // 公文删除
    $("#deleteDocument").click(function () {
        $.ajax({
            type: 'GET',//请求类型
            url: '../document/draft/delete/' + doc_id,//请求地址
            dataType: 'JSON',
            success: function (json,status){
                if(json.err_msg == null){
                    new PNotify({
                        title: '消息',
                        text: '您已删除这条公文',
                        type: 'success',
                        hide: false,
                        styling: 'bootstrap3'
                    });
                    window.location.href = "../document/my_draft_doc.html";
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(textStatus);
            },
            fail: function () { //如果在传输过程失败了：调用fail中对应的匿名函数(可选)
                console.log("fail!");
            },
        });
    })

});

//iframe自适应
function iFrameHeight() {
    var ifm= document.getElementById("documentIframe");
    var subWeb = document.frames ? document.frames["documentIframe"].document : ifm.contentDocument;
    if(ifm != null && subWeb != null) {
        //ifm.height = subWeb.body.scrollHeight;
        ifm.width = subWeb.body.scrollWidth;
    }
}
